package com.finance.logistics.anlyzier;


import com.finance.base.constant.BaseConstants;
import com.finance.framework.lang.Privilege;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.system.bean.pojo.User;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 配送员工号解析类
 * Created by CBWL on 2017/4/27.
 */
@Component
public class PostmanHandler extends AbstractHandler {

    @Override
    @Resource(name="deliverySiteHandler")
    public void setNextHandler(AbstractHandler nextHandler){
        super.setNextHandler(nextHandler);
    }

    @Override
    public void doHandler(LogisticsData logisticsData) {
        this.logger.info("待解析运单信息：{}", net.sf.json.JSONObject.fromObject(logisticsData).toString());
        String postmanNo = logisticsData.getPostmanNo();
        User postman = this.userService.getByCode(postmanNo);
        if(postman == null){
            this.logger.error("未知的运单配送员[postmanNo]:{}",postmanNo);
            String errorReason = "未知的运单配送员[postmanNo]:{"+ postmanNo+"}";
            this.logisticsErrorLogService.addErrorLog(logisticsData,errorReason, BaseConstants.ANALYZE_ERROR_TYPE_POSTMAN);
        }else{
            String tableName = this.logisticsDataService.getDataTableName(logisticsData.getSignTime());
            LogisticsData logisticsData2update = this.logisticsDataService.getByWaybillNum(tableName,logisticsData.getWaybillNum());
            logisticsData2update.setPostmanNo(postmanNo);
            try {
                this.logisticsDataService.update(logisticsData2update);
                this.updateDataVild(logisticsData2update,Privilege.B5);
                this.getNextHandler().doHandler(logisticsData);
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
            }
        }
    }
}
